package cn.jksoft.businessclient.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jksoft.businessclient.R;
import cn.jksoft.businessclient.bean.ProductEntity;

public class ProductSelectExpandableAdapter extends BaseExpandableListAdapter {
    private Context context;
    List<String> groups;
    private List<List<ProductEntity>> childs;
    ItemViewHolder itemViewHolder;

    public void setDeleteClickListener(OnDeleteClickListener deleteClickListener) {
        this.deleteClickListener = deleteClickListener;
    }

    private OnDeleteClickListener deleteClickListener;

    public class ItemViewHolder {
        @Bind(R.id.tv_service_price)
        TextView tvServicePrice;
        @Bind(R.id.tv_service_type)
        TextView tvServiceType;
        @Bind(R.id.iv_delete_service)
        ImageView ivDeleteService;

        public ItemViewHolder(View itemView) {
            ButterKnife.bind(this, itemView);
        }
    }

    public ProductSelectExpandableAdapter(Context context,
                                          List<String> groups,
                                          List<List<ProductEntity>> childs) {
        this.groups = groups;
        this.childs = childs;
        this.context = context;
    }


    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // 获取二级列表的View对象
    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ItemViewHolder itemViewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 获取二级列表对应的布局文件, 并将其各元素设置相应的属性
            convertView = layoutInflater.inflate(R.layout.item_select_product, null);
            itemViewHolder = new ItemViewHolder(convertView);
            this.itemViewHolder = itemViewHolder;
            convertView.setTag(itemViewHolder);
        } else {
            itemViewHolder = (ItemViewHolder) convertView.getTag();
        }
        ProductEntity entity = childs.get(groupPosition).get(childPosition);
        itemViewHolder.tvServicePrice.setText("￥" + Double.parseDouble(entity.getSALE_PRICE()) / 100);
        String storageName = entity.getSTORAGE_NAME()!=null && entity.getSTORAGE_NAME().length()>0?"/"+entity.getSTORAGE_NAME():"";
        itemViewHolder.tvServiceType.setText(entity.getPROD_NAME()+storageName);
        itemViewHolder.ivDeleteService.setTag(entity);
        itemViewHolder.ivDeleteService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteClickListener.OnClickListener(v,groupPosition,childPosition);
            }
        });
        return convertView;
    }


    @Override
    public int getChildrenCount(int groupPosition) {
        return childs.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // 获取一级列表View对象
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 获取一级列表布局文件,设置相应元素属性
            convertView = layoutInflater.inflate(R.layout.item_select_product_group, null);
        }
        TextView tvTitle = (TextView) convertView.findViewById(R.id.item_tv_title);
        tvTitle.setText(groups.get(groupPosition));
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public static interface OnDeleteClickListener {
        void OnClickListener(View view , int groupPosition,int childPosition);
    }
}
